/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiResultPesq.java
 *
 * Created on 23/11/2011, 23:19:23
 */

package GUI;
import java.util.Vector;
import javax.swing.JFrame;
import tp1POO.Persistencia.GerenciadorObjetos;
import tp1POO.Modelo.*;
import tp1POO.Verifica;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import tp1POO.GestorLog;
import tp1POO.Persistencia.Pesquisa;
/**
 *
 * @author matheus
 */
public class GuiResultPesq extends javax.swing.JFrame {

    /** Creates new form GuiResultPesq */
     public GuiResultPesq() {
        initComponents();
        Tipo tipo = new Tipo(" ");
        Vector<ObjetoPOO> obj = GerenciadorObjetos.obterInstancia().obterObjs(tipo);
        Vector<String> strs = new Vector<String>();
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();

        if (obj.size()!=0) {
            for (int i=0;i<obj.size();i++) {
                tipo = (Tipo)obj.get(i);
                jCTipos.insertItemAt(tipo, i);
            }
        }
        modelo.addColumn("Marca");
        modelo.addColumn("Modelo");
        modelo.addColumn("Cor");
        modelo.addColumn("Placa");
        modelo.addColumn("Opcionais");
        modelo.addColumn("Observações");
    }

    public GuiResultPesq(int pesq) {
        initComponents();
        jPTiposSelect.remove(jBPesquisar);
        jPTiposSelect.remove(jCTipos);
        switch (pesq){
            case 0: carregaListaVeiculoDisp();
                    break;
            case 1: carregaListaVeiculoMaisAlug();
                    break;
            case 2: carregaListaVeiculoRent();
                    break;
            case 3: carregaListaLocacaoAberto();
                    break;
            case 4: carregaListaLocacaoFechadas();
                    break;
        }


        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPPesqVeic = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        jPTiposSelect = new javax.swing.JPanel();
        jCTipos = new javax.swing.JComboBox();
        jBPesquisar = new javax.swing.JButton();
        jBCancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Resultados da Pesquisa");

        jPPesqVeic.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable);

        javax.swing.GroupLayout jPPesqVeicLayout = new javax.swing.GroupLayout(jPPesqVeic);
        jPPesqVeic.setLayout(jPPesqVeicLayout);
        jPPesqVeicLayout.setHorizontalGroup(
            jPPesqVeicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPPesqVeicLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 498, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(7, Short.MAX_VALUE))
        );
        jPPesqVeicLayout.setVerticalGroup(
            jPPesqVeicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPPesqVeicLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                .addContainerGap())
        );

        jCTipos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCTiposActionPerformed(evt);
            }
        });

        jBPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/ico_lupa.gif"))); // NOI18N
        jBPesquisar.setText("Pesquisar");
        jBPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPesquisarActionPerformed(evt);
            }
        });

        jBCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/icoErro.gif"))); // NOI18N
        jBCancel.setText("Fechar");
        jBCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPTiposSelectLayout = new javax.swing.GroupLayout(jPTiposSelect);
        jPTiposSelect.setLayout(jPTiposSelectLayout);
        jPTiposSelectLayout.setHorizontalGroup(
            jPTiposSelectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPTiposSelectLayout.createSequentialGroup()
                .addContainerGap(95, Short.MAX_VALUE)
                .addGroup(jPTiposSelectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPTiposSelectLayout.createSequentialGroup()
                        .addComponent(jCTipos, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jBCancel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPTiposSelectLayout.setVerticalGroup(
            jPTiposSelectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPTiposSelectLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPTiposSelectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCTipos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBPesquisar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jBCancel))
        );

        jPTiposSelectLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jBPesquisar, jCTipos});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPPesqVeic, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jPTiposSelect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPPesqVeic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPTiposSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents



    private void carregaListaVeiculoDisp () {
        Veiculo veiculo = new Veiculo(" ");
        Vector<Veiculo> obj = Pesquisa.veiculosDisponiveis();
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();

        modelo.addColumn("Marca");
        modelo.addColumn("Modelo");
        modelo.addColumn("Cor");
        modelo.addColumn("Placa");
        modelo.addColumn("Opcionais");
        modelo.addColumn("Observações");
        if (obj.size()!=0) {
            for (int i=0;i<obj.size();i++) {
                veiculo = (Veiculo)obj.get(i);
                modelo.addRow(new String[] {veiculo.getMarca(),veiculo.getModelo(),veiculo.getCor(),veiculo.getPlaca(),veiculo.getOpcionais(),veiculo.getObservacoes()});
            }
        }
        jLabel1.setText("Veículos Disponíveis :");
    }

    private void carregaListaVeiculoRent () {
        Veiculo veiculo = new Veiculo(" ");
        Vector<Veiculo> veiculos = Pesquisa.veiculosMaisRentaveis();
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();

        modelo.addColumn("Marca");
        modelo.addColumn("Modelo");
        modelo.addColumn("Cor");
        modelo.addColumn("Placa");
        modelo.addColumn("Renda obtida");
        if (veiculos.size()!=0) {
            for (int i=0;i<veiculos.size();i++) {
                veiculo = veiculos.get(i);
                int soma=0;
                for (Locacao loc : veiculo.getLocacoes())
			soma += loc.getValor();
                modelo.addRow(new String[] {veiculo.getMarca(),veiculo.getModelo(),veiculo.getCor(),veiculo.getPlaca(),"R$ "+String.valueOf(soma)});
            }
        }
        jLabel1.setText("Veículos mais Rentáveis :");
    }

    private void carregaListaVeiculoMaisAlug () {
        Veiculo veiculo = new Veiculo(" ");
        Vector<Veiculo> veiculos = Pesquisa.veiculosMaisAlugados();
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();

        modelo.addColumn("Marca");
        modelo.addColumn("Modelo");
        modelo.addColumn("Cor");
        modelo.addColumn("Placa");
        modelo.addColumn("N. de Locações");
        if (veiculos.size()!=0) {
            for (int i=0;i<veiculos.size();i++) {
                veiculo = veiculos.get(i); 
                modelo.addRow(new String[] {veiculo.getMarca(),veiculo.getModelo(),veiculo.getCor(),veiculo.getPlaca(),String.valueOf(veiculo.getLocacoes().size())});
            }
        }
        jLabel1.setText("Veículos mais Alugados :");
    }

    private void carregaListaVeiculoCetegoria (String categoria) {
        Veiculo veiculo = new Veiculo(" ");
        Vector<Veiculo> veiculos = Pesquisa.veiculosCategoria(categoria);
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();

        
        if (veiculos.size()!=0) {
            for (int i=0;i<veiculos.size();i++) {
                veiculo = veiculos.get(i);
                modelo.addRow(new String[] {veiculo.getMarca(),veiculo.getModelo(),veiculo.getCor(),veiculo.getPlaca(),veiculo.getOpcionais(),veiculo.getObservacoes()});
            }
        }
        jLabel1.setText("Veículos da Categoria :");
    }

    private void carregaListaLocacaoAberto () {
        Locacao loc = null;
        Vector<Locacao> locacoes = Pesquisa.locacoesEmAberto();
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();

        modelo.addColumn("Cliente");
        modelo.addColumn("Marca");
        modelo.addColumn("Modelo");
        modelo.addColumn("Data de Saída");
        if (locacoes.size()!=0) {
            for (int i=0;i<locacoes.size();i++) {
                loc = locacoes.get(i);
                modelo.addRow(new String[] {loc.getLocador().getNome(),loc.getVeiculo().getMarca(),loc.getVeiculo().getModelo(),loc.getDataSaida()});
            }
        }
        jLabel1.setText("Locações em aberto :");
    }

    private void carregaListaLocacaoFechadas () {
        Locacao loc = null;
        Vector<Locacao> locacoes = Pesquisa.locacoesFechadas();
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();

        modelo.addColumn("Cliente");
        modelo.addColumn("Marca");
        modelo.addColumn("Modelo");
        modelo.addColumn("Data de Saída");
        if (locacoes.size()!=0) {
            for (int i=0;i<locacoes.size();i++) {
                loc = locacoes.get(i);
                modelo.addRow(new String[] {loc.getLocador().getNome(),loc.getVeiculo().getMarca(),loc.getVeiculo().getModelo(),loc.getDataSaida(),loc.getDataDevolucao(),String.valueOf(loc.getValor())});
            }
        }
        jLabel1.setText("Locações fechadas :");
    }








   






    private void jBCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelActionPerformed
        GestorLog.obterInstancia().inserirMsgLog("Funcionalidade de Pesquisar foi fechada("+GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+")");
        GuiCreator.obterInstancia().removerGuiResultPesq();
    }//GEN-LAST:event_jBCancelActionPerformed

    private void jBPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPesquisarActionPerformed
        Tipo tipo = (Tipo)jCTipos.getSelectedItem();
        DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();
        if (tipo != null) {
            while (modelo.getRowCount()>0) {
                modelo.removeRow(0);
            }
            carregaListaVeiculoCetegoria(tipo.getNome());
            GestorLog.obterInstancia().inserirMsgLog(GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+
                        " - Pesquisa realizada");
        }
        else {
            JOptionPane.showMessageDialog(jPPesqVeic,"ERRO : Nenhum tipo selecionado!", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBPesquisarActionPerformed

    private void jCTiposActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCTiposActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCTiposActionPerformed
















    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GuiResultPesq().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCancel;
    private javax.swing.JButton jBPesquisar;
    private javax.swing.JComboBox jCTipos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPPesqVeic;
    private javax.swing.JPanel jPTiposSelect;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable;
    // End of variables declaration//GEN-END:variables

}
